// *************************************************************************************************
//
// MK3D Engine
// --------------------------------------
// Copyright (C) 2007-2011 Zeb (mailto: zebbey@gmail.com)
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// *************************************************************************************************

#include "mk_header.h"
#include "mk_mp_specular.h"
#include "mk_scene.h"
#include "mk_engine.h"
#include "mk_render_device.h"
#include "mk_shader.h"

namespace MK3DE
{
	CMPSpecular::CMPSpecular()
		: _specularColor(0.5f, 0.5f, 0.5f, 1.0f)
	{
		_shiness = 30.0f;
	}

	void CMPSpecular::SetSpecular(BOOL bEnable, D3DXCOLOR& specular, float shiness)
	{
		_bNeedToSet = bEnable;
		if (bEnable)
		{
			_specularColor = specular;
			_shiness = shiness;
		}
	}

	void CMPSpecular::GetSpecular(BOOL& bEnable, D3DXCOLOR& specular, float& shiness)
	{
		bEnable = _bNeedToSet;
		specular = _specularColor;
		shiness = _shiness;
	}

	void CMPSpecular::OnSetShaderConstant(CShader* pShader, CScene* pScene)
	{
		if (_bNeedToSet)
		{
			CLightDirection* pDirectionLight = pScene->GetDirectionLight();
			D3DXCOLOR specular = pDirectionLight->GetSpecular() * pDirectionLight->GetBrightness();
			D3DXColorModulate(&specular, &specular, &_specularColor);
			pShader->SetFloatArray(SCN_SPECULAR_LIGHT_COLOR, (float*)&specular, 4);
			pShader->SetFloatArray(SCN_SHININESS, &_shiness, 1);
		}
	}
}